from socket import *
from tools import get_ip_address

class DataUpdater:

    def __init__(self, host: str, port: int):
        self.addr = (host, port)
        self.udp_cli_sock = socket(AF_INET, SOCK_DGRAM)
        self.udp_cli_sock.bind((get_ip_address(), 0))
        self.udp_cli_sock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)

    def broadcast_status(self, json_data: str):
        print("sending -> %s" % json_data)
        self.udp_cli_sock.sendto(json_data.encode(), self.addr)
